IF exists(SELECT * FROM dbo.sysobjects WHERE name='maakHulptabelRooster' AND xtype='P') 
BEGIN
	DROP PROCEDURE maakHulptabelRooster
END
GO

create procedure maakHulptabelRooster
as
delete from hulptabelRooster

insert into hulptabelRooster( uur, maandag, dinsdag, woensdag, donderdag, vrijdag, hetLokaal )
select u.uur, ' ',' ',' ',' ',' ', l.hetLokaal
from uren as u, deLokalen as l
order by l.hetLokaal,u.uur

update hulptabelRooster
set maandag = m.deKlas,dinsdag = d.deKlas,woensdag = w.deKlas,donderdag = do.deKlas,vrijdag=v.deKlas
from  hulptabelRooster as u 
	left join (select top 100 percent * from rooster 
		where deDag = 'Ma'
		order by deKlas) as m
	  on u.Uur = m.hetUur and u.hetLokaal = m.hetLokaal
 	left join (select top 100 percent * from rooster 
		where deDag = 'Di'
		order by deKlas) as d	  
	  on u.Uur = d.hetUur and u.hetLokaal = d.hetLokaal 
	left join (select top 100 percent * from rooster 
		where deDag = 'Wo'
		order by deKlas) as w
	  on u.Uur = w.hetUur and u.hetLokaal = w.hetLokaal
 	left join (select top 100 percent * from rooster 
		where deDag = 'Do'
		order by deKlas) as do
	  on u.Uur = do.hetUur and u.hetLokaal = do.hetLokaal 
	left join (select top 100 percent * from rooster 
		where deDag = 'Vr'
		order by deKlas) as v
	  on u.Uur = v.hetUur and u.hetLokaal = v.hetLokaal